<?php
namespace app\common\model;
use think\Model;

class CartModel extends Model
{
    protected $name = 'cart';




    /**
     * 购物车数量
     * @ApiMethod GET
     */
    public function getCartCount($user_id)
    {
        try {
            $where = [
                'user_id' => $user_id,
            ];
            // 处理找不到规格的商品
            $data = $this
                ->with(['goods' => function($query){
                    $query->field('id,goods_name,image');
                },'spec' => function($query){
                    $query->field('goods_spec_id,spec_item_value,spec_image,spec_price');
                }])
                ->where($where)
                ->whereNull('deletetime')
                ->select();
            foreach ($data as $row){
                if(!$row->spec) {
                    $row->deletetime = time();
                    $row->save();
                }
            }

            $count = $this
                ->where($where)
                ->whereNull('deletetime')
//                ->sum('num');
                ->count();
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => [
                    'count' => $count
                ]
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }

    }


    /**
     * 购物车列表
     *
     * @param $user_id int 用户ID
     * @param $page int 页码
     * @param $limit int 每页数据
     */
    public function getPageList($user_id,$page,$limit)
    {
        try {
            $field = ['id','goods_id','goods_spec_id','num'];
            $where = [
                'user_id' => $user_id,
            ];

            $data = $this
                ->with(['goods' => function($query){
                    $query->field('id,goods_name,image');
                },'spec' => function($query){
                    $query->field('goods_spec_id,spec_item_value,spec_image,spec_price,spec_stock');
                }])
                ->field($field)
                ->where($where)
                ->whereNull('deletetime')
                ->paginate($limit,false,['page' => $page]);

            foreach ($data as $key => $row){
                if($row->spec){
                    if($row->goods){
                        $row->goods->image = ImageAddDomain($row->goods->image);
                    }
                    $row->spec->spec_image = ImageAddDomain($row->spec->spec_image);
                }else{
                    unset($data[$key]);
                }
            }
            if($data){
                $data = $data->toArray();
                sort($data['data']);
            }
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => $data
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => $e->getLine()
            ];
        }
    }

    /**
     * 加入购物车
     *
     * @param $user_id int 用户ID
     * @param $goods_id int 商品ID
     * @param $goods_spec_id int 商品规格ID
     * @param $num int 商品数量
     */
    public function addCart($params,$user_id)
    {
        extract($params);
        try {
            $data = $this
                ->where([
                    'user_id' => $user_id,
                    'goods_id' => $goods_id,
                    'goods_spec_id' => $goods_spec_id
                ])
                ->whereNull('deletetime')
                ->find();
            if($data){
                // 合并数量
                $data->num += $num;
                $data->save();
            }else{
                $this->create([
                    'user_id' => $user_id,
                    'goods_id' => $goods_id,
                    'goods_spec_id' => $goods_spec_id,
                    'num' => $num,
                    'createtime' => time(),
                ]);
            }
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => []
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }


    /**
     * 删除
     *
     * @param $user_id int 用户ID
     * @param $cart_ids string 购物车ID集
     */
    public function delCartGoods($params,$user_id)
    {
        extract($params);
        try {
            $this
            ->where(['user_id' => $user_id])
            ->whereIn('id',$cart_ids)
            ->setField(['deletetime' => time()]);
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => []
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }

    }

    /**
     * 数量修改
     *
     * @param $user_id int 用户ID
     * @param $cart_id int 购物车ID
     * @param $num int 数量
     */
    public function editCartGoodsNum($params,$user_id)
    {
        extract($params);

        try {
            $res = $this
                ->where([
                    'user_id' => $user_id,
                    'id' => $cart_id
                ])
                ->whereNull('deletetime')
                ->update(['num' => $num]);
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => []
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }




    public function goods()
    {
        return $this->belongsTo('app\common\model\goods\GoodsModel', 'goods_id', 'id');
    }
    public function spec()
    {
        return $this->belongsTo('app\common\model\goods\GoodsSpecModel', 'goods_spec_id', 'goods_spec_id');
    }

}